﻿// Project: WebPage, File: WebPageFactory.cs
// Namespace: WebPage, Class: WebPageFactory
// Path: G:\Secure Project\WebCrawlerProject\WebPage, Author: Radu-Mihail
// Code lines: 86, Size of file: 3.38 KB
// Creation date: 1/9/2009 12:11 AM
// Last modified: 1/11/2009 5:04 PM

#region Using directives
using System;
using WebPage.Client;
using Robots;
#endregion

namespace WebPage
{
    /// <summary>
    /// Web page factory
    /// </summary>
    public static class WebPageFactory
    {       
        /// <summary>
        /// Get web page
        /// </summary>
        /// <param name="link">Link</param>
        /// <param name="maxSize">Maximum size, -1 -> do not take in consideration</param>
        /// <returns>Full web page</returns>
        
        public static AbstractWebPage GetWebPage(string link, int maxSize)
        {
            try
            {
                Boolean revisit = false;
                MyWebClient webClient = new MyWebClient(link);
                #region Check database and robots
                Database.DatabaseWork databaseConnection = new Database.DatabaseWork();
                if (maxSize != -1)
                    if (webClient.ContentSize > maxSize)
                        return null;
                if (databaseConnection.PageExists(link))
                {
                    //check if current page exists in our database, and if it does check the last update field to see if we should redownload it
                    if (!databaseConnection.isModified(webClient.GetLastModifiedDate(), link))
                    {
                        return null;
                    } // if ()
                    else
                    {
                        ParseRobots robotCheck = new ParseRobots(webClient.HostLink);
                        Boolean isAllowed = true;
                        foreach (string positionCheck in robotCheck.DisallowedZones)
                        {
                            if (link.Contains(positionCheck))
                            {
                                isAllowed = false;
                                break;
                            } // if (link.Contains)
                        } // foreach (positionCheck)
                        if (false == isAllowed)
                        {
                            foreach (string positionCheck in robotCheck.AllowedZones)
                            {
                                if (link.Contains(positionCheck))
                                {
                                    isAllowed = true;
                                    break;
                                } // if (link.Contains)
                            } // foreach (positionCheck)
                        } // if (false)

                        if (false == isAllowed)
                            return null;

                        revisit = true;
                    } // else
                    //check if there are any restrictions to this page in the servers robots.txt
                } // if (databaseConnection.PageExists)
                #endregion
                
                switch (webClient.GetContentType())
                {
                    //case WebPage.Client.WebPageContentType.IMAGE:
                    //    return new ImageWebPage(ref webClient, revisit);
                    //case WebPage.Client.WebPageContentType.TEXT:
                    //    return new TextWebPage(ref webClient, revisit);
                    case WebPage.Client.WebPageContentType.TEXTHTML:
                        return new HTMLTextWebPage(ref webClient, revisit);
                    default:
                        webClient.Dispose();
                        return null;

                } // switch                 
            }
            catch (Exception ex)
            {
                //Add exception to log and return null;
                Error.ErrorManagement.ManageException(ex);
                return null;
            }
        } // GetWebPage(link, maxSize)
    } // class WebPageFactory
} // namespace WebPage
